package com.mingtu.dzpatrol.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * @ClassName : IntentUtils
 * @Author : wkk
 * @Date : 2020/5/6 09:25:26
 * @Email : 1281573890@qq.com
 * @Eescription :界面跳转
 */
public class IntentUtils {

    private static IntentUtils instance;
    private static Intent intent;

    // 单例模式中获取唯一的IntentUtils实例
    public static synchronized IntentUtils getInstance() {
        if (null == instance) {
            instance = new IntentUtils();
        }

        intent = new Intent();
        return instance;
    }

    /**
     * 界面跳转
     *
     * @param cls 目标Activity
     */
    public void readyGo(Activity activity, Class<?> cls) {
        readyGo(activity, cls,null);
    }

    /**
     * 跳转界面，传参
     *
     * @param cls    目标Activity
     * @param bundle 数据
     */
    public void readyGo(Activity activity, Class<?> cls, Bundle bundle) {
        intent = new Intent(activity, cls);
        if (null != bundle)
            intent.putExtras(bundle);
        activity.startActivity(intent);
        intent = null;
    }

    /**
     * 跳转界面并关闭当前界面
     *
     * @param cls 目标Activity
     */
    public void readyGoThenKill(Activity activity, Class<?> cls) {
        readyGoThenKill(activity, cls, null);
    }

    /**
     * @param cls    目标Activity
     * @param bundle 数据
     */
    public void readyGoThenKill(Activity activity, Class<?> cls, Bundle bundle) {
        readyGo(activity, cls, bundle);
        activity.finish();
    }

    /**
     * startActivityForResult
     *
     * @param cls         目标Activity
     * @param requestCode 发送判断值
     */
    public void readyGoForResult(Activity activity, Class<?> cls, int requestCode) {
        intent = new Intent(activity, cls);
        activity.startActivityForResult(intent, requestCode);
        intent = null;
    }

    /**
     * startActivityForResult with bundle
     *
     * @param cls         目标Activity
     * @param requestCode 发送判断值
     * @param bundle      数据
     */
    public void readyGoForResult(Activity activity, Class<?> cls, int requestCode, Bundle bundle) {
        intent = new Intent(activity, cls);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        activity.startActivityForResult(intent, requestCode);
        intent = null;
    }
}
